package com.nike.util;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;

import com.nike.bean.AppChromeInfo;
import com.nike.bean.NikeHttpBean;

public class NikeAppHttpClientUtil {
	public static NikeHttpBean nikeHttpGet(String url,String referer,NikeHttpBean nikeHttpBean) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
    			.header("User-Agent",nikeHttpBean.getPhone_type_name())
    			.header("Accept",AppChromeInfo.INDEX_ACCEPT)
    			.header("Referer",referer)
    			.header("Cookie", nikeHttpBean.getBm_sz()+";"+nikeHttpBean.get_abck())
    			.GET()
                .build();
		List<String> cookies = httpGetRequest.headers().allValues("Cookie");
		for(String temp : cookies) {
			System.out.println("before request:"+temp);
		}
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofString())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
                System.out.println(resp.version());
                System.out.println(resp.statusCode());
//                System.out.println(resp.body());
                List<String> list = resp.headers().allValues("set-cookie");
                
                for(String temp : list) {
                	System.out.println(temp);
                	if(temp.startsWith("bm_sz")) {
                		nikeHttpBean.setBm_sz(temp.split(";")[0]);
                	}else if(temp.startsWith("_abck")) {
                		nikeHttpBean.set_abck(temp.split(";")[0]);
                	}
                }
                nikeHttpBean.setReturnContent(resp.body());
            }
        }).join();
        
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpGet_2(String url,String referer,NikeHttpBean nikeHttpBean) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
    			.header("User-Agent",nikeHttpBean.getPhone_type_name())
    			.header("Accept",AppChromeInfo.INDEX_ACCEPT)
    			.header("Referer",referer)
    			.GET()
                .build();
		List<String> cookies = httpGetRequest.headers().allValues("Cookie");
		for(String temp : cookies) {
			System.out.println("before request:"+temp);
		}
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofString())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
                System.out.println(resp.version());
                System.out.println(resp.statusCode());
//                System.out.println(resp.body());
                List<String> list = resp.headers().allValues("set-cookie");
                for(String temp : list) {
                	System.out.println(temp);
                	if(temp.startsWith("bm_sz")) {
                		nikeHttpBean.setBm_sz(temp.split(";")[0]);
                	}else if(temp.startsWith("_abck")) {
                		nikeHttpBean.set_abck(temp.split(";")[0]);
                	}
                }
                nikeHttpBean.setReturnContent(resp.body());
            }
        }).join();
        
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpPost(String url,String referer,String type,String request_Body,NikeHttpBean nikeHttpBean) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		String contentType = "";
		if("plain".equals(type)) {
			contentType = AppChromeInfo.CONTENT_TYPE_PLAIN;
		}else if("json".equals(type)) {
			contentType = AppChromeInfo.CONTENT_TYPE_JSON;
		}
		HttpRequest httpPostRequest = HttpRequest.newBuilder(uri)
    			.header("Content-Type",contentType)
    			.header("User-Agent",nikeHttpBean.getPhone_type_name())
    			
    			.header("Accept","*/*")
    			.header("Accept-Language",AppChromeInfo.ACCEPT_LANGUAGE)
    			.header("Accept-Encoding",AppChromeInfo.ACCEPT_ENCODING)
    			.header("Referer",referer)
//    			.header("Origin","https://s3.nikecdn.com")
    			.header("Cookie", nikeHttpBean.getBm_sz())
    			
                .POST(HttpRequest.BodyPublishers.ofString(request_Body))
                .build();
		List<String> cookies = httpPostRequest.headers().allValues("Cookie");
		for(String temp : cookies) {
			System.out.println("before request:"+temp);
		}
		
		//sendAsync
//		httpClient.sendAsync(httpPostRequest, HttpResponse.BodyHandlers.ofString())
//        .whenComplete((resp,t) -> {
//            if(t != null){
//                t.printStackTrace();
//            }else{
//                System.out.println(resp.version());
//                System.out.println(resp.statusCode());
//                System.out.println(resp.body());
//                List<String> list = resp.headers().allValues("set-cookie");
//                for(String temp : list) {
//                	System.out.println(temp);
//                	if(temp.startsWith("bm_sz")) {
//                		nikeHttpBean.setBm_sz(temp.split(";")[0]);
//                	}else if(temp.startsWith("_abck")) {
//                		nikeHttpBean.set_abck(temp.split(";")[0]);
//                	}
//                }
//                nikeHttpBean.setReturnContent(resp.body());
//            }
//        }).join();
		
		HttpResponse<String> response =
				httpClient.send(httpPostRequest, HttpResponse.BodyHandlers.ofString());
		System.out.println(response.version());
		System.out.println(response.statusCode());
		System.out.println(response.body());
		List<String> list = response.headers().allValues("set-cookie");
	      for(String temp : list) {
	      	System.out.println(temp);
	      	if(temp.startsWith("bm_sz")) {
	      		nikeHttpBean.setBm_sz(temp.split(";")[0]);
	      	}else if(temp.startsWith("_abck")) {
	      		nikeHttpBean.set_abck(temp.split(";")[0]);
	      	}
	      }
      nikeHttpBean.setReturnContent(response.body());
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpPostLogined(String url,String referer,String type,String request_Body,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		String contentType = "";
		if("plain".equals(type)) {
			contentType = AppChromeInfo.CONTENT_TYPE_PLAIN;
		}else if("json".equals(type)) {
			contentType = AppChromeInfo.CONTENT_TYPE_JSON;
		}
		HttpRequest httpPostRequest = HttpRequest.newBuilder(uri)
    			.header("Content-Type",contentType)
    			.header("User-Agent",nikeHttpBean.getPhone_type_name())
    			.header("Authorization","Bearer "+userMap.get("access_token"))
    			.header("Accept","*/*")
    			.header("Accept-Language",AppChromeInfo.ACCEPT_LANGUAGE)
    			.header("Accept-Encoding",AppChromeInfo.ACCEPT_ENCODING)
    			.header("Referer",referer)
//    			.header("Origin","https://s3.nikecdn.com")
    			.header("Cookie", nikeHttpBean.getBm_sz()+";"+nikeHttpBean.get_abck())
    			
                .POST(HttpRequest.BodyPublishers.ofString(request_Body))
                .build();
		List<String> cookies = httpPostRequest.headers().allValues("Cookie");
		for(String temp : cookies) {
			System.out.println("before request:"+temp);
		}
		
		//sendAsync
//		httpClient.sendAsync(httpPostRequest, HttpResponse.BodyHandlers.ofString())
//        .whenComplete((resp,t) -> {
//            if(t != null){
//                t.printStackTrace();
//            }else{
//                System.out.println(resp.version());
//                System.out.println(resp.statusCode());
//                System.out.println(resp.body());
//                List<String> list = resp.headers().allValues("set-cookie");
//                for(String temp : list) {
//                	System.out.println(temp);
//                	if(temp.startsWith("bm_sz")) {
//                		nikeHttpBean.setBm_sz(temp.split(";")[0]);
//                	}else if(temp.startsWith("_abck")) {
//                		nikeHttpBean.set_abck(temp.split(";")[0]);
//                	}
//                }
//                nikeHttpBean.setReturnContent(resp.body());
//            }
//        }).join();
		
		HttpResponse<String> response =
				httpClient.send(httpPostRequest, HttpResponse.BodyHandlers.ofString());
		System.out.println(response.version());
		System.out.println(response.statusCode());
		System.out.println(response.body());
		List<String> list = response.headers().allValues("set-cookie");
	      for(String temp : list) {
	      	System.out.println(temp);
	      	if(temp.startsWith("bm_sz")) {
	      		nikeHttpBean.setBm_sz(temp.split(";")[0]);
	      	}else if(temp.startsWith("_abck")) {
	      		nikeHttpBean.set_abck(temp.split(";")[0]);
	      	}
	      }
      nikeHttpBean.setReturnContent(response.body());
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpPut(String url,String referer,String type,String request_Body,NikeHttpBean nikeHttpBean,String access_token) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		String contentType = "";
		String accept = "";
		if("plain".equals(type)) {
			contentType = AppChromeInfo.CONTENT_TYPE_PLAIN;
		}else if("json".equals(type)) {
			contentType = AppChromeInfo.CONTENT_TYPE_JSON;
			accept = AppChromeInfo.CONTENT_TYPE_JSON;
		}
		HttpRequest httpPostRequest = HttpRequest.newBuilder(uri)
    			.header("Content-Type",contentType)
    			.header("User-Agent",nikeHttpBean.getPhone_type_name())
    			.header("Accept",accept)
    			.header("Accept-Language",AppChromeInfo.ACCEPT_LANGUAGE)
    			.header("Accept-Encoding",AppChromeInfo.ACCEPT_ENCODING)
    			.header("appid","com.nike.commerce.snkrs.web")
    			.header("authorization","Bearer "+access_token)
    			.header("Referer",referer)
                .POST(HttpRequest.BodyPublishers.ofString(request_Body))
                .build();
		List<String> cookies = httpPostRequest.headers().allValues("Cookie");
		for(String temp : cookies) {
			System.out.println("before request:"+temp);
		}
		httpClient.sendAsync(httpPostRequest, HttpResponse.BodyHandlers.ofString())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
                System.out.println(resp.version());
                System.out.println(resp.statusCode());
                System.out.println(resp.body());
                List<String> list = resp.headers().allValues("set-cookie");
                for(String temp : list) {
                	System.out.println(temp);
                }
                nikeHttpBean.setReturnContent(resp.body());
            }
        }).join();
        
		return nikeHttpBean;
	}
}
